using System.Net;
using System.Security.Cryptography.Xml;
using System.Text.RegularExpressions;

namespace 获得对应页面
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            dataBox.Text = "暂无页面数据";
        }
        string webString;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                HttpWebRequest request = WebRequest.Create(urlBox.Text) as HttpWebRequest;
                WebResponse response = request.GetResponse();
                Stream resStream = response.GetResponseStream();
                StreamReader sr = new StreamReader(resStream);
                dataBox.Text = webString = sr.ReadToEnd();
            }
            catch (Exception)
            {
                MessageBox.Show("输入的地址格式错误！");
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //邮箱格式的正则表达式
                string validEm = @"[a-zA-Z0-9]+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*[a-zA-Z0-9]";
                //手机号码格式的正则表达式
                string validPh = @"[1]+[3,4,5,7,8]+\d{9}";
                emailBox.Clear();
                //邮箱进行正则匹配
                foreach (Match match in Regex.Matches(webString, validEm))
                {
                    emailBox.Text = emailBox.Text + match.ToString() + "\r\n";
                }
                phoneBox.Clear();
                //电话进行正则匹配
                foreach (Match match in Regex.Matches(webString, validPh))
                {
                    phoneBox.Text = phoneBox.Text + match.ToString() + "\r\n";
                }
                //无邮箱/电话情境
                if (emailBox.Text.Equals(""))
                {
                    emailBox.Text = "当前爬取的页面无邮箱！";
                }
                if (phoneBox.Text.Equals(""))
                {
                    phoneBox.Text = "当前爬取的页面无电话！";
                }
            }
            catch (Exception)
            { }
        }
    }
}
